﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Framework.Core;
using Framework.Platform.Components.UI.View;

namespace Framework.Platform.Components.UI.MainForm
{
    public partial class ApplicationForm : Form, IApplicationMainUI
    {
        public ApplicationForm()
        {
            InitializeComponent();
        }

        private IDictionary<string, DockContent> _Views = new Dictionary<string, DockContent>();

        #region IApplicationMainUI Members

        private IList<INavigator> _Navigators;

        public IList<INavigator> Navigators
        {
            get
            {
                if (_Navigators == null)
                {
                    _Navigators = new List<INavigator>();
                }
                return this._Navigators;
            }
            set
            {
                this._Navigators = value;
            }
        }

        private IList<IModuleView> _ModuleViews;

        public IList<IModuleView> ModuleViews
        {
            get
            {
                if (_ModuleViews == null)
                {
                    _ModuleViews = new List<IModuleView>();
                }
                return _ModuleViews;
            }
            set
            {
                _ModuleViews = value;
            }
        }

        public void HideUI()
        {
            this.Hide();
        }

        public void ShowUI()
        {
            this.Show();
        }

        public event EventHandler<EventArgs> ApplicationClosed;

        public void AddModuleView(IModuleView moduleView)
        {
            this.AddView(moduleView);
        }

        public void RemoveModuleView(string moduleViewKey)
        {
            throw new NotImplementedException();
        }
        #endregion

        protected void OnApplicationClosed(EventArgs e)
        {
            if (ApplicationClosed != null)
            {
                ApplicationClosed(this, e);
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            this.OnApplicationClosed(e);
        }

        private void ApplicationMainForm_Load(object sender, EventArgs e)
        {
            foreach (INavigator item in this.Navigators)
            {
                this.AddNavigator(item);
            }
            foreach (IModuleView item in this.ModuleViews)
            {
                this.AddModuleView(item);
            }
        }

        private void AddView(IView view)
        {
            try
            {
                if (!(view is UserControl))
                {
                    throw new Exception("视图必须是UserControl。");
                }

                DockContent content;
                DockState dockState;
                if (this._Views.ContainsKey(view.ViewKey))
                {
                    content = this._Views[view.ViewKey];
                    dockState = content.DockState;
                }
                else
                {
                    content = new DockContent();
                    content.CloseButton = view.Closeable;
                    content.TabText = view.ViewName;
                    content.Tag = view;
                    content.ControlBox = true;
                    switch (view.Position)
                    {
                        case ViewPosition.Left:
                            dockState = DockState.DockLeft;
                            break;
                        case ViewPosition.Right:
                            dockState = DockState.DockRight;
                            break;
                        case ViewPosition.Top:
                            dockState = DockState.DockTop;
                            break;
                        case ViewPosition.Bottom:
                            dockState = DockState.DockBottom;
                            break;
                        case ViewPosition.Middle:
                            dockState = DockState.Document;
                            break;
                        case ViewPosition.Float:
                            dockState = DockState.Float;
                            break;
                        default:
                            dockState = DockState.Unknown;
                            break;
                    }
                    ((UserControl)view).Dock = DockStyle.Fill;
                    content.Controls.Add((UserControl)view);
                    content.HideOnClose = true;
                }
                content.Show(this.MainDockPanel, dockState);
                if (!this._Views.ContainsKey(view.ViewKey))
                {
                    this._Views.Add(view.ViewKey, content);
                }
                else
                {
                    throw new Exception("试图已经存在。");
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void AddNavigator(INavigator navigator)
        {
            this.AddView(navigator);
            navigator.ViewEventRaise += new EventHandler<ViewEventArgs>(navigator_ViewEventRaise);
            //navigator.SelectedChanged += new EventHandler<NavigatorSelectedChangedEventArgs>(navigator_SelectedChanged);
        }

        void navigator_ViewEventRaise(object sender, ViewEventArgs e)
        {
            switch (e.ViewEventKey)
            {
                case NavigatorEvents.OnSelectedChanged:
                    navigator_SelectedChanged(sender, e);
                    break;
                default:
                    break;
            }
        }

        private void navigator_SelectedChanged(object sender, ViewEventArgs e)
        {
            NavigatorSelectedChangedEventArgs arg = (NavigatorSelectedChangedEventArgs)e;
            IView view = ObjectFactory.GetObject(arg.NewItem.Link) as IView;
            if (!this._Views.ContainsKey(view.ViewKey))
            {
                if (string.IsNullOrEmpty(view.ViewName))
                {
                    view.ViewName = arg.NewItem.Text;
                }
                this.AddView(view);
            }
            else
            {
                this._Views[view.ViewKey].Activate();
            }
        }
    }
}
